import ajaxAsync from '../../../utils/ajaxAsync';

/**
 * 查询票价
 *
 * @param {Number} subrouteID 线路id
 * @param {Number} direction 方向id
 */
function getPrice(subrouteID, direction) {
  return ajaxAsync('/bus/route/today_price', {
    SUB_ROUTEID: parseInt(subrouteID),
    BUS_DIRECTION: parseInt(direction)
  }).then(res => {
    let { PRICE } = res
    return { price: PRICE }
  })
}

/**
 * 获取首末班车时间
 *
 * @param {Number} subrouteID 线路id
 * @param {Number} direction 方向id
 * @returns
 */
function getFirstAndLastBusTime(subrouteID, direction) {
  return ajaxAsync('/bus/route/today_schedule', {
    SUB_ROUTEID: parseInt(subrouteID),
    BUS_DIRECTION: parseInt(direction)
  }).then(res => {
    let { FIRST_TIME, LAST_TIME } = res
    return { firstTime: FIRST_TIME, lastTime: LAST_TIME }
  })
}

/**
 * 下一班公交车时间查询
 *
 * @param {Number} subrouteID
 * @param {Number} direction
 */
function getNextBusTime(subrouteID, direction) {
  let nextBusTime
  // 配置文件更改关闭下一班发车时间暂留
  // if (window.openNextTime && window.isBusNowApis) {
  return ajaxAsync('/bus/bus/planRunTime', {
    SUBROUTEID: subrouteID,
    DIRECTION: direction
  }).then(res => {
    if (res.NEXT_TIME) {
      nextBusTime = res.NEXT_TIME
    }
    return { nextBusTime }
  })
  // } else {
  //   return Promise.resolve({ nextBusTime })
  // }
  // }
}

/**
 * 获取公告详情
 *
 * @param {Number} subrouteID
 * @param {Number} direction
 * @returns
 */
function getNoticeDetail(subrouteID, direction) {
  return ajaxAsync('/bus/notice/detail/byRouteId', {
    SUB_ROUTEID: subrouteID,
    BUS_DIRECTION: direction
  }).then(res => {
    let noticeTitle = ''
    let noticeContent = ''
    let noticeUrl = ''
    if (res.NOTICE_ID) {
      noticeTitle = res.NOTICE_TITLE
      noticeContent = res.NOTICE_CONTENT
      noticeUrl = res.GOTO
      // TODO:提示详情，web存缓存，RN暂留
      // window.LINE_NOTICE = JSON.stringify({
      //   NOTICE_ID: res.NOTICE_ID,
      //   NOTICE_TITLE: res.NOTICE_TITLE,
      //   NOTICE_CONTENT: res.NOTICE_CONTENT,
      //   NOTICE_TIME: dateFtt('yyyy-MM-dd hh:mm', res.NOTICE_TIME),
      //   SEGMENT_NAME_LIST: res.SEGMENT_NAME_LIST,
      //   GOTO: res.GOTO
      // })
    }
    return { noticeTitle, noticeContent, noticeUrl }
  })
}

export { getPrice, getFirstAndLastBusTime, getNextBusTime, getNoticeDetail }
